/*
package com.itheima.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@RabbitListener(queues = "queue_demo01")
public class MyRabbitListener {
    @RabbitHandler
    public void msg(Message message, Channel channel, String msg) {
        try {
            //模拟接收消息
            System.out.println("消费端接收消息"+msg);
            //模拟处理业务
            System.out.println("处理中...");
            Thread.sleep(2000);
            System.out.println("处理完成...");
            //手动处理消息。basicAck确认签收
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            //如果出现异常，则拒绝消息 可以重回队列 也可以丢弃 可以根据业务场景来
            e.printStackTrace();
            //拒绝签收，不批量处理，重回队列
            try {
                //判断是否已被消费但没被签收
                if (message.getMessageProperties().getRedelivered()) {
                    System.out.println("该消息已被消费过，但没有签收");
                }
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
*/
